package com.sunwayworld.dev.http;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;





public class HttpTools {

	private String httpserver;

	public HttpTools(String server) {
		httpserver = server;
	}

	public InputStream httpConnect() throws MalformedURLException, IOException {
		URL url = new URL(httpserver);
		URLConnection uc = url.openConnection();
		return uc.getInputStream();
	}

	public InputStream httpPost(String str) throws MalformedURLException, IOException {
		URL url = new URL(httpserver);
		URLConnection uc = url.openConnection();
		uc.setDoOutput(true);
		
		OutputStreamWriter out = new OutputStreamWriter(uc.getOutputStream());
		out.write(str);
		out.flush(); 
		out.close();
		return uc.getInputStream();
	}
	public InputStream httpPost2(String str) throws MalformedURLException, IOException {
		URL url = new URL(httpserver);
		HttpURLConnection uc=(HttpURLConnection)url.openConnection();
		uc.setUseCaches(true);
		uc.setDoOutput(true);
		uc.setDoInput(true);
		uc.setRequestMethod("POST");
		uc.setRequestProperty("Content-type","text/xml");
		byte[] pSendBytes= str.getBytes("GBK");
		DataOutputStream out = new DataOutputStream(uc.getOutputStream());
		out.write(pSendBytes);
		out.flush(); 
		
		return uc.getInputStream();
	}
	

}

